#include <windows.h>
#include <windowsx.h>
#include <gdiplus.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

using namespace Gdiplus;

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
					 LPSTR lpszCmdParam,int nCmdShow)
{
	HWND hWnd;
	MSG Message;
	WNDCLASS WndClass;
	LPSTR lpszClass = "im2";

	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;

	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	
	WndClass.cbClsExtra=0;
	WndClass.cbWndExtra=0;
	WndClass.hbrBackground = static_cast<HBRUSH> (GetStockObject(WHITE_BRUSH));
	WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
	WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	WndClass.hInstance = hInstance;
	WndClass.lpfnWndProc = static_cast<WNDPROC> (WndProc);
	WndClass.lpszClassName = lpszClass;
	WndClass.lpszMenuName = NULL;
	WndClass.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&WndClass);

	hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL, static_cast <HMENU>(NULL),
		hInstance, NULL);
	ShowWindow(hWnd, nCmdShow);

	while(GetMessage(&Message,0,0,0)) {
		TranslateMessage(&Message);
		DispatchMessage(&Message);
	}

	GdiplusShutdown(gdiplusToken);
	return static_cast<int> (Message.wParam);
}

int cx, cy;

void OnSize(HWND hwnd, UINT state, int cx, int cy);
void OnPaint(HWND hwnd);
void OnDestroy(HWND hwnd);

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		HANDLE_MSG(hwnd, WM_SIZE, OnSize);
		HANDLE_MSG(hwnd, WM_PAINT, OnPaint);
		HANDLE_MSG(hwnd, WM_DESTROY, OnDestroy);
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

void OnSize(HWND hwnd, UINT state, int cx, int cy)
{
	::cx = cx;
	::cy = cy;
}

void OnPaint(HWND hwnd)
{
	PAINTSTRUCT ps;
	HDC hdc;

	hdc = BeginPaint(hwnd, &ps);

	{
		FontFamily fontFamily(L"Times New Roman");
		Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
		RECT rc;
		GetClientRect(hwnd, &rc);
		RectF rcf(0, 0, static_cast<REAL> (rc.right), static_cast<REAL> (rc.bottom));

		StringFormat format;
		format.SetAlignment(StringAlignmentCenter);
		format.SetLineAlignment(StringAlignmentCenter);

		SolidBrush brush(Color(127, 255, 0, 0));

		Graphics graphics(hdc);
		graphics.DrawString(L"hello", -1, &font, rcf, &format, &brush);
		EndPaint(hwnd, &ps);
	}
}

void OnDestroy(HWND hwnd)
{
	PostQuitMessage(0);
}